//
//  SISSEnrollTenantViewModel.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/18.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSEnrollTenantViewModel.h"
#import "SISSContractMutiSelectCell.h"
#import "SISSContractTwoFillInTypeTwoCell.h"
#import "UILabel+ChangeLineSpaceAndWordSpace.h"

@implementation SISSEnrollTenantViewModel

- (NSString *)plistName {
    return @"TenantEnrollDataSourceArray";
}

- (void)registContractClass:(UITableView *)tableView {
    [super registContractClass:tableView];
    
    [tableView registerClass:[SISSContractMutiSelectCell class] forCellReuseIdentifier:contractMutiSelectCellReuseID];
    [tableView registerClass:[SISSContractTwoFillInTypeTwoCell class] forCellReuseIdentifier:contractTwoFillInTypeTwoCellReuseID];
}

- (UITableViewCell *)getCell:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    
    NSInteger cellType = self.dataArr[indexPath.section][indexPath.row].cellType;
    if (cellType == 14) {
        SISSContractMutiSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:contractMutiSelectCellReuseID forIndexPath:indexPath];
        cell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        cell.isHiddenIconImage = YES;
        cell.isHiddenBottomTextView = YES;
        return cell;
    } else if (cellType == 19) {
        SISSContractTwoFillInTypeTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:contractTwoFillInTypeTwoCellReuseID forIndexPath:indexPath];
        cell.contractModel = self.dataArr[indexPath.section][indexPath.row];
        return cell;
    }
    return [super getCell:tableView indexPath:indexPath];
}

- (UIView *)getViewHeaderInSection:(NSInteger)section {
    
    SISSBaseView *headerView = [[SISSBaseView alloc] init];
    headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    UILabel *label = [[UILabel alloc] init];
    label.textColor = COLOR(0x334A60);
    label.font = [UIFont systemFontOfSize:16];
    label.numberOfLines = 0;
    [headerView addSubview:label];
    
    CGFloat margin = 8.0;
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView).offset(2 * margin);
        make.right.equalTo(headerView).offset(-2 * margin);
        make.centerY.equalTo(headerView);
    }];
    
    if (section == 0) {
        label.text = @"温馨提示：请确保您登记的信息真实有效，发现弄虚作假的将记录不良行为。";
        [UILabel changeLineSpaceForLabel:label WithSpace:8];
    } else if (section == 1) {
        label.text = @"所需配置";
    } else if (section == 2) {
        label.text = @"租房要求";
    } else {
        label.text = @"";
    }
    
    return headerView;
}

- (CGFloat)getHeightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 66;
    } else if (section == 3) {
        return 10;
    }
    return 44;
}

- (SISSTenantModel *)dataArrayToTenantModel {
    SISSTenantModel *tenantModel = [[SISSTenantModel alloc] init];
    
    tenantModel.region = self.dataArr[0][0].region ? self.dataArr[0][0].region : @"";
    tenantModel.regionDetail = self.dataArr[0][0].regionDetail ? self.dataArr[0][0].regionDetail : @"";
    tenantModel.neighbourhood = self.dataArr[0][1].rightStr ? self.dataArr[0][1].rightStr : @"";
    tenantModel.minPrice = self.dataArr[0][2].rightStr ? self.dataArr[0][2].rightStr : @"";
    tenantModel.maxPrice = self.dataArr[0][2].rightTwoStr ? self.dataArr[0][2].rightTwoStr : @"";
    tenantModel.houseType = self.dataArr[0][3].rightStr ? self.dataArr[0][3].rightStr : @"";
    tenantModel.residents = self.dataArr[0][4].rightStr ? [self.dataArr[0][4].rightStr substringToIndex:(self.dataArr[0][4].rightStr.length - 1)] : @"";
    tenantModel.checkinTime = self.dataArr[0][5].rightStr ? self.dataArr[0][5].rightStr : @"";
    tenantModel.houseConfigure = self.dataArr[1][0].rightStr ? self.dataArr[1][0].rightStr : @"";
    tenantModel.houseFeature = self.dataArr[2][0].rightStr ? self.dataArr[2][0].rightStr : @"";
    tenantModel.direction = self.dataArr[2][1].rightStr ? self.dataArr[2][1].rightStr : @"";
    tenantModel.decoration = self.dataArr[2][2].rightStr ? self.dataArr[2][2].rightStr : @"";
    tenantModel.otherdesc = self.dataArr[2][3].rightStr ? self.dataArr[2][3].rightStr : @"";
    tenantModel.renterName = self.dataArr[3][0].rightStr ? self.dataArr[3][0].rightStr : @"";
    tenantModel.idNumber = self.dataArr[3][1].rightStr ? self.dataArr[3][1].rightStr : @"";
    tenantModel.mobile = self.dataArr[3][2].rightStr ? self.dataArr[3][2].rightStr : @"";
    
    return tenantModel;
}

- (void)assignmentWithTenantModel:(SISSTenantModel *)tenantModel {
    
    NSDictionary *draftDict = [tenantModel yy_modelToJSONObject];
    
    for (NSInteger section = 0; section < self.dataArr.count; section++) {
        
        for (NSInteger idx = 0; idx < self.dataArr[section].count; idx++) {
            
            SISSContractModel *obj = self.dataArr[section][idx];
            
            if (section == 0 && idx == 0) {
                obj.rightStr = tenantModel.region.length > 0 ? [NSString stringWithFormat:@"%@-%@",tenantModel.region,tenantModel.regionDetail] : @"";
                obj.region = tenantModel.region;
                obj.regionDetail = tenantModel.regionDetail;
            } else if (section == 0 && idx == 2) {
                obj.rightStr = tenantModel.minPrice ? tenantModel.minPrice : @"";
                obj.rightTwoStr = tenantModel.maxPrice ? tenantModel.maxPrice : @"";
            } else if (section == 0 && idx == 4) {
                obj.rightStr = tenantModel.residents ? [NSString stringWithFormat:@"%@人",tenantModel.residents] : @"";
            } else {
                obj.rightStr = draftDict[obj.propertyKey] != nil ? [NSString stringWithFormat:@"%@",draftDict[obj.propertyKey]] : @"";
                
            }
        }
    }
    
    [self.tableView reloadData];
}

@end
